home *** CD-ROM | disk | FTP | other *** search
- Path: raffles.technet.sg!usenet
- From: cheehui <cheehui@aztech.com.sg>
- Newsgroups: comp.lang.c
- Subject: Re: Turbo C 3.0 - arithmetic in defines
- Date: Fri, 16 Feb 1996 09:57:42 +0800
- Organization: PMO
- Message-ID: <3123E496.145B@aztech.com.sg>
- References: <4fqgkf$iqd@ccshst05.cs.uoguelph.ca> <4fre9v$kcs@news.nstn.ca> <4fvphq$t06@druid.borland.com>
- NNTP-Posting-Host: 202.42.226.169
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (Win95; I)
-
- >thay@uoguelph.ca (Toby K Hay) wrote:
- >#define var1 16
- >#define var2 485
- >#define var3 ((var2-1)/var1)+1
- >int main(void)
- > {
- > printf("\n%i %i %i %i",var1,var2,var3,var1*var3);
- > return 0;
- > }
- >
- >Output from this is '16 485 31 481', not '16 485 31 496' as I should have
- >expected. That is, var3 is calculated correctly in its #define, but the
- >var1*var3 in the printf() statement is not calculated correctly. Where
- >am I going wrong?
-
- Try this instead :
- #define var3 ((var2-1)/var1+1)
-
- The reason for the failure is precedence.
- The compiler takes the last variable as:
-
- var1 * (var2-1)/var1+1
-
- which is (var1*(var2-1)/var1)+1 which gives 16*30 +1 = 481
-
- QED.
-
- Regards
- Chee Hui :)
- e-mail:cheehui@aztech.com.sg
-